package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.model.LatLng;
import com.sgb.alimaplibrary.MarkerEntity;
import com.sgb.alimaplibrary.widget.MapMarkerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPackersMapBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.packers.PackersMapEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.view.widget.map.PackersMapWindow;
import com.sgb.kjwl.viewmodel.packers.PackersMapModel;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author 毁灭霸者
 * @date 2020/7/14
 * description:地图查找
 */
@Deprecated
public class PackersMapActivity extends AppActivity {
    private ActivityPackersMapBinding binding;

    private int type = 0;
    @Inject
    PackersMapModel viewModel;
    private final List<PackersMapEntity> workOptionsList = new ArrayList<>();
    private final List<PackersMapEntity> teamOptionsList = new ArrayList<>();
    List<MarkerEntity> markerEntityList = new ArrayList<>();
    private LatLng latLng;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, PackersMapActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, int type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.setClass(context, PackersMapActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.setClass(context, PackersMapActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder()
                .appComponent(appComponent)
                .packersModule(new PackersModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initMap(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PackersMapActivity.this, R.layout.activity_packers_map);
        binding.workLayout.setOnClickListener(this);
        binding.parkerLayout.setOnClickListener(this);
    }

    @Override
    protected void init() {
        type = getIntent().getIntExtra("type", 0);
    }

    private void initMap(Bundle savedInstanceState) {
        binding.mapView.initMap(savedInstanceState, getLifecycle(), new MapMarkerView.CallBack() {
            @Override
            public void onMarkerClick(int pos, String title) {
                showMarkerView(pos);
            }

            @Override
            public void onLocation(Double lat, Double log, String string) {
                binding.mapView.deactivate();
            }
        });
        viewModel.getLocationModel();
    }

    public void showLocation(MapLongAndLatEntity longLat) {
        latLng = new LatLng(Double.parseDouble(longLat.getLat()), Double.parseDouble(longLat.getLng()));
        initTabLayout();
    }

    private void initTabLayout() {
        if (type == 0) {
            binding.workView.setSelected(true);
            binding.workLine.setVisibility(View.VISIBLE);
            binding.parkerView.setSelected(false);
            binding.parkerLine.setVisibility(View.INVISIBLE);
            viewModel.getWorkList();
        } else {
            binding.workView.setSelected(false);
            binding.workLine.setVisibility(View.INVISIBLE);
            binding.parkerView.setSelected(true);
            binding.parkerLine.setVisibility(View.VISIBLE);
            viewModel.getPackerList();
        }
    }

    public void showMarkerView(int pos) {
        PackersMapWindow packersMapWindow = new PackersMapWindow(this);
        if (type == 0) {
            packersMapWindow.setMarker(R.mipmap.parcker_map_tips2);
            packersMapWindow.setWorkData(workOptionsList.get(pos));
        } else if (type == 1) {
            packersMapWindow.setMarker(R.mipmap.parcker_map_tips);
            packersMapWindow.setTeamData(teamOptionsList.get(pos));
        }
        PackersMapEntity packersMapEntity = packersMapWindow.getData();
        if (packersMapEntity != null) {
            if (binding.workLine.getVisibility() == View.VISIBLE) {
                MapFindWorkActivity.start(PackersMapActivity.this, packersMapEntity.getName(), packersMapEntity.getId());
            } else {
                MapFindPackersActivity.start(PackersMapActivity.this, packersMapEntity.getName(), packersMapEntity.getId());
            }
        }
    }

    public void showWorkList(List<PackersMapEntity> list) {
        workOptionsList.clear();
        workOptionsList.addAll(list);
        binding.mapView.setMyLocation(latLng.latitude, latLng.longitude,R.mipmap.current_spot);
        addMarker(workOptionsList);
    }

    public void showTeamList(List<PackersMapEntity> list) {
        teamOptionsList.clear();
        teamOptionsList.addAll(list);
        binding.mapView.setMyLocation(latLng.latitude, latLng.longitude,R.mipmap.current_spot);
        addMarker(teamOptionsList);
    }

    private void addMarker(List<PackersMapEntity> list) {
        markerEntityList.clear();
        for (PackersMapEntity entity : list) {
            LatLng latLng = new LatLng(Double.parseDouble(entity.getLat()), Double.parseDouble(entity.getLng()));
            PackersMapWindow mapInfoWindow = new PackersMapWindow(this);
            if (type == 0) {
                mapInfoWindow.setMarker(R.mipmap.parcker_map_tips2);
                mapInfoWindow.setWorkData(entity);
            } else {
                mapInfoWindow.setMarker(R.mipmap.parcker_map_tips);
                mapInfoWindow.setTeamData(entity);
            }
            markerEntityList.add(new MarkerEntity(latLng,mapInfoWindow,entity.getId()));
        }
        binding.mapView.addMarkerData(markerEntityList);

        binding.mapView.zoomToSpan(latLng);
    }



    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.work_layout) {
            type = 0;
            initTabLayout();
        } else if (id == R.id.parker_layout) {
            type = 1;
            initTabLayout();
        }
    }
}
